package charpter13.lynx;

import java.util.Formatter;

public class Four {
	private double total = 0;
	private Formatter f = new Formatter(System.out);
	private int itemWidth = 15;
	private int qtyWidth = 5;
	private int priceWidth = 10;
	private String stringFmt = "%-" + itemWidth + "s %" + qtyWidth + "s %" + priceWidth
			+ "s\n";
	private String numericFmt = "%-" + itemWidth + "." + itemWidth + "s %" + qtyWidth
			+ "d %" + priceWidth + ".2f\n";
	private String numericStringFmt = "%-" + itemWidth + "." + itemWidth + "s %" + qtyWidth
			+ "s %" + priceWidth + ".2f\n";

	public void printTitle() {
		
		f.format(stringFmt, "Item", "Qty", "Price");
		f.format(stringFmt, "----", "---", "-----");
	}

	public void print(String name, int qty, double price) {
		f.format(numericFmt, name, qty, price);
		total += price;
	}

	public void printTotal() {
		f.format(numericStringFmt, "Tax", "", total * 0.06);
		f.format(stringFmt, "", "", "-----");
		f.format(numericStringFmt, "Total", "", total * 1.06);
	}

	public static void main(String[] args) {
		Four receipt = new Four();
		receipt.printTitle();
		receipt.print("Jack's Magic Beans", 4, 4.25);
		receipt.print("Princess Peas", 3, 5.1);
		receipt.print("Three Bears Porridge", 1, 14.29);
		receipt.printTotal();
	}
}
